<template>
  <div class="friends-container">
    <div class="friends-header">
      <h1>好友管理</h1>
      <div class="header-actions">
        <el-button type="primary" @click="showAddFriendDialog">添加好友</el-button>
        <el-button @click="goToFriendRequests">好友请求</el-button>
        <el-button @click="manageGroups">分组管理</el-button>
        <el-button @click="refreshFriends">刷新</el-button>
      </div>
    </div>
    
    <div class="friends-content">
      <div class="search-box">
        <el-input
          v-model="searchQuery"
          placeholder="搜索好友"
          prefix-icon="el-icon-search"
          clearable
        />
      </div>
      
      <div class="friends-list">
        <el-collapse v-model="activeNames">
          <el-collapse-item title="好友列表 (15)" name="friends">
            <div class="friend-item" v-for="friend in filteredFriends" :key="friend.id">
              <img :src="friend.avatar" :alt="friend.name" class="friend-avatar" />
              <div class="friend-info">
                <div class="friend-name">{{ friend.name }}</div>
                <div class="friend-details">
                  <span class="friend-role">{{ friend.role }}</span>
                  <span class="friend-signature">{{ friend.signature }}</span>
                </div>
              </div>
              <div class="friend-status">
                <span :class="['status-dot', friend.online ? 'online' : 'offline']"></span>
                <span class="status-text">{{ friend.online ? '在线' : '离线' }}</span>
              </div>
              <div class="friend-actions">
                <el-button size="small" @click="chatWithFriend(friend)">聊天</el-button>
                <el-button size="small" @click="deleteFriend(friend)">删除</el-button>
                <el-button size="small" @click="editRemark(friend)">备注</el-button>
              </div>
            </div>
          </el-collapse-item>
          
          <el-collapse-item title="同学 (8)" name="classmates">
            <div class="friend-item" v-for="friend in classmates" :key="friend.id">
              <img :src="friend.avatar" :alt="friend.name" class="friend-avatar" />
              <div class="friend-info">
                <div class="friend-name">{{ friend.name }}</div>
                <div class="friend-details">
                  <span class="friend-role">{{ friend.role }}</span>
                  <span class="friend-signature">{{ friend.signature }}</span>
                </div>
              </div>
              <div class="friend-status">
                <span :class="['status-dot', friend.online ? 'online' : 'offline']"></span>
                <span class="status-text">{{ friend.online ? '在线' : '离线' }}</span>
              </div>
              <div class="friend-actions">
                <el-button size="small" @click="chatWithFriend(friend)">聊天</el-button>
                <el-button size="small" @click="deleteFriend(friend)">删除</el-button>
                <el-button size="small" @click="editRemark(friend)">备注</el-button>
              </div>
            </div>
          </el-collapse-item>
          
          <el-collapse-item title="同事 (5)" name="colleagues">
            <div class="friend-item" v-for="friend in colleagues" :key="friend.id">
              <img :src="friend.avatar" :alt="friend.name" class="friend-avatar" />
              <div class="friend-info">
                <div class="friend-name">{{ friend.name }}</div>
                <div class="friend-details">
                  <span class="friend-role">{{ friend.role }}</span>
                  <span class="friend-signature">{{ friend.signature }}</span>
                </div>
              </div>
              <div class="friend-status">
                <span :class="['status-dot', friend.online ? 'online' : 'offline']"></span>
                <span class="status-text">{{ friend.online ? '在线' : '离线' }}</span>
              </div>
              <div class="friend-actions">
                <el-button size="small" @click="chatWithFriend(friend)">聊天</el-button>
                <el-button size="small" @click="deleteFriend(friend)">删除</el-button>
                <el-button size="small" @click="editRemark(friend)">备注</el-button>
              </div>
            </div>
          </el-collapse-item>
          
          <el-collapse-item title="家人 (2)" name="family">
            <div class="friend-item" v-for="friend in family" :key="friend.id">
              <img :src="friend.avatar" :alt="friend.name" class="friend-avatar" />
              <div class="friend-info">
                <div class="friend-name">{{ friend.name }}</div>
                <div class="friend-details">
                  <span class="friend-role">{{ friend.role }}</span>
                  <span class="friend-signature">{{ friend.signature }}</span>
                </div>
              </div>
              <div class="friend-status">
                <span :class="['status-dot', friend.online ? 'online' : 'offline']"></span>
                <span class="status-text">{{ friend.online ? '在线' : '离线' }}</span>
              </div>
              <div class="friend-actions">
                <el-button size="small" @click="chatWithFriend(friend)">聊天</el-button>
                <el-button size="small" @click="deleteFriend(friend)">删除</el-button>
                <el-button size="small" @click="editRemark(friend)">备注</el-button>
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </div>
    
    <!-- 添加好友对话框 -->
    <el-dialog title="添加好友" v-model="addFriendDialogVisible" width="400px">
      <el-form>
        <el-form-item label="用户名或邮箱">
          <el-input v-model="newFriendInfo" placeholder="请输入用户名或邮箱"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addFriendDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="sendFriendRequest">发送好友请求</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Friends",
  data() {
    return {
      activeNames: ['friends', 'classmates', 'colleagues', 'family'],
      searchQuery: "",
      addFriendDialogVisible: false,
      newFriendInfo: "",
      friends: [
        { id: 1, name: "张三", role: "班长", signature: "今天的阳光真好", avatar: require('@/assets/default-avatar.png'), online: true },
        { id: 2, name: "李四", role: "同学", signature: "正在忙...", avatar: require('@/assets/default-avatar.png'), online: false },
        { id: 3, name: "王五", role: "同事", signature: "(无)", avatar: require('@/assets/default-avatar.png'), online: true },
        // 更多好友数据...
      ],
      classmates: [
        { id: 4, name: "赵六", role: "同学", signature: "学习使我快乐", avatar: require('@/assets/default-avatar.png'), online: true },
        { id: 5, name: "钱七", role: "同学", signature: "今天天气不错", avatar: require('@/assets/default-avatar.png'), online: false },
        // 更多同学数据...
      ],
      colleagues: [
        { id: 6, name: "孙八", role: "同事", signature: "工作第一", avatar: require('@/assets/default-avatar.png'), online: true },
        { id: 7, name: "周九", role: "同事", signature: "加油干", avatar: require('@/assets/default-avatar.png'), online: true },
        // 更多同事数据...
      ],
      family: [
        { id: 8, name: "吴十", role: "家人", signature: "家和万事兴", avatar: require('@/assets/default-avatar.png'), online: true },
        { id: 9, name: "郑一", role: "家人", signature: "幸福美满", avatar: require('@/assets/default-avatar.png'), online: false },
        // 更多家人数据...
      ]
    };
  },
  computed: {
    filteredFriends() {
      if (!this.searchQuery) return this.friends;
      return this.friends.filter(friend => 
        friend.name.includes(this.searchQuery) || 
        friend.role.includes(this.searchQuery) ||
        friend.signature.includes(this.searchQuery)
      );
    }
  },
  methods: {
    showAddFriendDialog() {
      this.addFriendDialogVisible = true;
    },
    
    sendFriendRequest() {
      if (!this.newFriendInfo) {
        this.$message.warning("请输入用户名或邮箱");
        return;
      }
      
      // 模拟发送好友请求
      this.$message.success(`已向 ${this.newFriendInfo} 发送好友请求`);
      this.addFriendDialogVisible = false;
      this.newFriendInfo = "";
    },
    
    goToFriendRequests() {
      this.$router.push("/friends/requests");
    },
    
    manageGroups() {
      this.$message.info("分组管理功能待实现");
    },
    
    refreshFriends() {
      this.$message.success("好友列表已刷新");
    },
    
    chatWithFriend(friend) {
      this.$message.info(`与 ${friend.name} 开始聊天`);
      // 这里应该跳转到聊天页面
    },
    
    deleteFriend(friend) {
      this.$confirm(`确定要删除好友 ${friend.name} 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success(`已删除好友 ${friend.name}`);
        // 这里应该调用API删除好友
      }).catch(() => {
        // 用户取消删除
      });
    },
    
    editRemark(friend) {
      this.$prompt(`请输入 ${friend.name} 的备注名`, '修改备注', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue: friend.name
      }).then(({ value }) => {
        this.$message.success(`已将 ${friend.name} 的备注修改为 ${value}`);
        // 这里应该调用API修改备注
      }).catch(() => {
        // 用户取消修改
      });
    }
  }
};
</script>

<style scoped>
.friends-container {
  padding: 20px;
  height: 100%;
  overflow-y: auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
}

.friends-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.friends-header h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.friends-content {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.search-box {
  margin-bottom: 20px;
}

.friend-item {
  display: flex;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.friend-item:last-child {
  border-bottom: none;
}

.friend-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 15px;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.friend-info {
  flex: 1;
}

.friend-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 5px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.friend-details {
  display: flex;
  gap: 10px;
}

.friend-role {
  font-size: 12px;
  color: #666;
  background-color: rgba(255, 255, 255, 0.3);
  padding: 2px 6px;
  border-radius: 3px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.friend-signature {
  font-size: 12px;
  color: #666;
  font-style: italic;
}

.friend-status {
  display: flex;
  align-items: center;
  margin-right: 20px;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 5px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.status-dot.online {
  background-color: #52c41a;
  box-shadow: 0 0 8px rgba(82, 196, 26, 0.5);
}

.status-dot.offline {
  background-color: #ccc;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.status-text {
  font-size: 12px;
  color: #666;
}

.friend-actions {
  display: flex;
  gap: 10px;
}

.friend-actions .el-button {
  background: rgba(255, 255, 255, 0.3);
  border: none;
  color: #333;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.friend-actions .el-button:hover {
  background: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.friend-actions .el-button--primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* 移动端优化 */
@media (max-width: 767px) {
  .friends-container {
    padding: var(--spacing-sm);
  }
  
  .friends-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--spacing-sm);
    margin-bottom: var(--spacing-sm);
  }
  
  .friends-header h1 {
    font-size: 20px;
  }
  
  .header-actions {
    width: 100%;
    flex-wrap: wrap;
  }
  
  .header-actions .el-button {
    flex: 1;
    font-size: 13px;
    padding: 8px 12px;
    margin-bottom: var(--spacing-xs);
  }
  
  .friends-content {
    padding: var(--spacing-sm);
  }
  
  .search-box {
    margin-bottom: var(--spacing-sm);
  }
  
  .friend-item {
    flex-direction: column;
    align-items: flex-start;
    padding: var(--spacing-sm) 0;
  }
  
  .friend-avatar {
    width: 40px;
    height: 40px;
    margin-right: 0;
    margin-bottom: var(--spacing-sm);
  }
  
  .friend-info {
    width: 100%;
    margin-bottom: var(--spacing-sm);
  }
  
  .friend-name {
    font-size: 15px;
    margin-bottom: var(--spacing-xs);
  }
  
  .friend-details {
    flex-direction: column;
    gap: var(--spacing-xs);
  }
  
  .friend-role {
    font-size: 11px;
  }
  
  .friend-signature {
    font-size: 11px;
  }
  
  .friend-status {
    margin-right: 0;
    margin-bottom: var(--spacing-sm);
  }
  
  .friend-actions {
    align-self: flex-end;
  }
  
  .friend-actions .el-button {
    font-size: 12px;
    padding: 4px 8px;
  }
}
</style>